package org.example.controller;

import org.example.pojo.Result;
import org.example.pojo.User;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("/user")
public class UserController {
    @PostMapping("/login")
    public Result login(User user, HttpSession session) {
        Result result = new Result();
        if (user != null && "123".equals(user.getPassWord())) {
            result.setFlag(true);
            // 保存用户名
            session.setAttribute("user", user.getUserName());
        }else {
            result.setFlag(false);
            result.setMessage("用户名或密码错误");
        }
        return result;
    }


    /**
     * 获取用户名
     * @param session
     * @return
     */

    @PostMapping("/getUserName")
    public String getUserName(HttpSession session) {
        return (String) session.getAttribute("user");
    }

}
